package com.salim.leetcode.$59;

public class SpiralMatrixII {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int total = n*n;
        int count = 1;
        int i=0;
        int j=0;
        while (count<=total){
            //右
            while (j<n && result[i][j]==0){
                result[i][j] = count;
                j++;
                count++;
            }
            j--;
            i++;
            //下
            while (i<n && result[i][j]==0){
                result[i][j] = count;
                i++;
                count++;
            }
            i--;
            j--;
            //左
            while (j>=0 && result[i][j]==0){
                result[i][j] = count;
                j--;
                count++;
            }
            j++;
            i--;
            //上
            while (i>=0 && result[i][j]==0){
                result[i][j] = count;
                i--;
                count++;
            }
            i++;
            j++;
        }
        return result;
    }
}
